<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

require_once dirname(__FILE__).'/menu.php';

class Honda_val_record_list extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Hondacustomermodel','m_customer');
    }

    public function index(){
        $data = parent::index();

        global $g_honda_card_type,$g_honda_game_gift;
        $data['card_type'] = array4js($g_honda_card_type);
        $data['game_gift'] = array4js($g_honda_game_gift);

        $this->load->view('admin/honda_val_record_list',$data);
    }

    public function getRecordList() {
        $assist['where'] = getFilter($this->input->post(),Hondacustomermodel::$recordSearchField);
        $assist['like'] = getFilter($this->input->post(),Hondacustomermodel::$recordLikeField);
        $assist['pagination'] = getPagination($this->input->post());

        $user = $this->session->userdata('user');
        // 4s店
        if($user['group_id'] == 2) {
            $assist['where']['honda_val_record.user_id'] = $this->m_app->getCurrentUserId();
        }

        $assist['select'] = '
            honda_customer.customer_id,
            honda_customer.openid,
            honda_customer.gift_type,
            honda_customer.gift_status,
            honda_val_record.*,
            honda_card.*,
            user.user_code,
            user.user_name,
            user.shop_phone,
            user.user_city,
        ';
        list($list,$count) = $this->m_customer->getRecordListSection($assist);

        // foreach ($list as $key => $value) {
        //     $list[$key]['nickname'] = urldecode($value['nickname']);
        // }

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function markGiftActive() {
        $this->validate->setRule('customer_id','integer');
        $this->validate->setRule('customer_id','message',$this->lang->line('error_parameter'));
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $user = $this->session->userdata('user');
        // 4s店
        if($user['group_id'] != 2) {
            $this->response->setDetail('非4s店不能标记领取奖品!');
            $this->response->responseExit();
        }

        $customer = $this->m_customer->getCustomer($info['customer_id']);
        if(empty($customer)) {
            $this->response->setDetail('参数错误!');
            $this->response->responseExit();
        }
        if($customer['gift_status'] == STATUS_ACTIVE) {
            $this->response->setDetail('奖品已兑换!请确认!');
            $this->response->responseExit();
        }
        $lotteryHistory = $this->m_customer->getLotteryHistoryGift($customer['customer_id']);
        if(empty($lotteryHistory)) {
            $this->response->setDetail('您未获奖!');
            $this->response->responseExit();
        }
        if($lotteryHistory['gift_status'] == STATUS_ACTIVE) {
            $this->response->setDetail('奖品已兑换!请确认!');
            $this->response->responseExit();
        }
        if($lotteryHistory['gift_type'] != 4) {
            $this->response->setDetail('店面只支持兑换奖品!');
            $this->response->responseExit();
        }

        $this->m_customer->editCustomer(array(
            'customer_id' => $customer['customer_id'],
            'gift_status' => STATUS_ACTIVE,
        ));
        $this->m_customer->editLotteryHistory(array(
            'lottery_history_id' => $lotteryHistory['lottery_history_id'],
            'user_id' => $this->m_app->getCurrentUserId(),
            'gift_status' => STATUS_ACTIVE,
            'lottery_time_time_active' => NOW,
        ));

        $this->response->setSuccess();
        $this->response->setDetail('更新成功!');
        $this->response->responseExit();
    }

    public function exportList() {
        @set_time_limit(0);
        @ini_set('memory_limit', '2048M');

        $assist['where'] = getFilter($this->input->get(),Hondacustomermodel::$recordSearchField);
        $assist['like'] = getFilter($this->input->get(),Hondacustomermodel::$recordLikeField);


        $this->load->library('excel');
        $this->excel->init('核销记录_'.date('YmdH').'.xls');
        $this->excel->addSheet('核销记录');
        $this->excel->addTitle(array(
            array('content'=>'店面编码'),
            array('content'=>'店面名称'),
            array('content'=>'顾客ID'),
            array('content'=>'openid'),
            array('content'=>'奖品'),
            array('content'=>'奖品状态'),
            array('content'=>'卡券类型'),
            array('content'=>'卡券号码'),
            array('content'=>'姓名'),
            array('content'=>'手机号'),
            array('content'=>'车架号'),
            array('content'=>'车型'),
            array('content'=>'核销时间'),
        ));
        
        $user = $this->session->userdata('user');
        // 4s店
        if($user['group_id'] == 2) {
            $assist['where']['honda_val_record.user_id'] = $this->m_app->getCurrentUserId();
        }

        $assist['select'] = '
            honda_customer.customer_id,
            honda_customer.openid,
            honda_customer.gift_type,
            honda_customer.gift_status,
            honda_val_record.*,
            honda_card.*,
            user.user_code,
            user.user_name,
            user.shop_phone,
            user.user_city,
        ';
        list($list,$count) = $this->m_customer->getRecordListSection($assist);


        global $g_honda_card_type,$g_honda_game_gift;

        $sex_array = array(
            0 => '未知',
            1 => '男',
            2 => '女',
        );
        $reservation_type_arr = array(
            '1' => '双快',
            '2' => '车主讲堂',
        );
        foreach ($list as $key => $value) {

            $this->excel->addRow(array(
                array('content' => $value['user_code']),
                array('content' => $value['user_name']),
                array('content' => $value['customer_id']),
                array('content' => $value['openid']),
                array('content' => id2name($value['gift_type'],$g_honda_game_gift)),
                array('content' => $value['gift_status'] == STATUS_ACTIVE ? '已领取': '未领取'),
                array('content' => id2name($value['card_type'],$g_honda_card_type)),
                array('content' => $value['card_number']),
                array('content' => $value['record_name']),
                array('content' => $value['record_phone']),
                array('content' => $value['record_frame_number']),
                array('content' => $value['record_models']),
                array('content' => $value['record_time_create']),
            ));
        }

        $this->excel->freezePanes(1,0);
        $this->excel->generate();
    }

}
/* End of file aa_game_history.php */
/* Location: ./application/controllers/aa_game_history.php */